//
//  LXCarOerderRecordViewController.m
//  ZuLinProject
//
//  Created by bancheng on 2022/2/15.
//  Copyright © 2021 LX_App. All rights reserved.
//

#import "LXCarOerderRecordViewController.h"
#import "LXCarOrderModel.h"
#import "LXCarOrederTableViewCell.h"
#import "LXCarOrderApplyViewController.h"
#import "LXCarOrderDetailViewController.h"



@interface LXCarOerderRecordViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic) UITableView *tableView;
@property (nonatomic) NSMutableArray *data;
@property (nonatomic) NSInteger pageIndex;
@end

@implementation LXCarOerderRecordViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    
    if (kUserDefaultForBool(kRefreshStr)) {
        kUserDefaultSetBool(NO, kRefreshStr);
        [self.tableView.mj_header beginRefreshing];
    }

    
}


- (void)viewDidLoad {
    [super viewDidLoad];
   
    
    [self.view addSubview:self.tableView];
    
    
    if (self.type==1) {
        [self layoutCustomNav:self.view title:@"下定记录"];
    }
    else {
        [self layoutCustomNav:self.view title:@"下定提醒"];
    }
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.pageIndex = 1;
        [self loadData];
    }];

    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
         self.pageIndex++;
        [self loadData];
    }];
    
    // 记录需要加header
    if (self.type == 1) {
        self.tableView.tableHeaderView = [self setHeaderView];
    }

   
    
}

- (void)loadData{

    NSString *url = @"";
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:@(self.pageIndex) forKey:@"page"];
    [dic setValue:@(20) forKey:@"rows"];
    
    if (self.type==1) {
        url = TBookingList_URL;
    }
    else if (self.type==2) {
        url = TBookingRemind_URL;
    }

    [MSNetworkManager postRevertUrl:url params:dic success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
       
        
        if ([self.tableView.mj_header isRefreshing]||self.pageIndex == 1) {
            [self.data removeAllObjects];
            [self.tableView.mj_footer resetNoMoreData];
        }
        NSInteger totalPage = [resonseObj[@"total"] integerValue];
            
        NSArray *records = resonseObj[@"records"];
        
        [self.data addObjectsFromArray:[LXCarOrderModel  mj_objectArrayWithKeyValuesArray:records]];
        
        
        if (self.data.count>=totalPage) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        
        
        [self endRefreshing];
        [self.tableView reloadData];
            
        [self isHaveEmptyData];
            
       
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
        [self endRefreshing];
        [self isHaveEmptyData];
    } withShowHud:YES];

    

}

-(UIView *)setHeaderView{
    UIView *headV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
    headV.backgroundColor = [UIColor colorWithHexString:@"#EBF0FE"];
    
    UILabel *carNumLb = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 100, headV.height)];
    carNumLb.text = @"车牌号";
    carNumLb.font = Fond14;
    carNumLb.textColor = mainColor;
    [headV addSubview:carNumLb];
    
    UILabel *customerLb = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth - 120, 0, 100, headV.height)];
    customerLb.text = @"签约客户";
    customerLb.font = Fond14;
    customerLb.textAlignment = NSTextAlignmentRight;
    customerLb.textColor = mainColor;
    [headV addSubview:customerLb];
    

    return headV;
    
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    
    static NSString *identifier = @"cell1";

    LXCarOrederTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([LXCarOrederTableViewCell class]) owner:self options:nil] objectAtIndex:0];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setClipsToBounds:YES];
    }

    
    LXCarOrderModel *model = self.data[indexPath.row];
    
    cell.carNumLb.text = [NSString stringWithFormat:@"%@ %@",model.brand,model.model];
    if (self.type==1) {
        cell.carRedLb.hidden = YES;
        cell.userNameLb.hidden = NO;
        cell.userNameLb.text = model.username;;
    }
    else {
        cell.carRedLb.hidden = NO;
        cell.userNameLb.hidden = YES;
        cell.carRedLb.text = model.count;

    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (self.type==1) {
        LXCarOrderApplyViewController *vc = [[LXCarOrderApplyViewController alloc] init];
        vc.orderModel = self.data[indexPath.row];
        vc.type = 2;
        [self pushToNextController:vc];
    }
    else {
        LXCarOrderDetailViewController *vc = [[LXCarOrderDetailViewController alloc] init];
        vc.orderModel = self.data[indexPath.row];
        [self pushToNextController:vc];
    }
   

}





- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, LL_StatusBarAndNavigationBarHeight, kScreenWidth, kScreenHeight-LL_StatusBarAndNavigationBarHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = kComHeight;
        _tableView.backgroundColor = ColorWithF5F5F5;
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

- (void)isHaveEmptyData{
    if (self.data.count == 0) {
        self.tableView.startTip = YES;
        self.tableView.tipView.backgroundColor = self.tableView.backgroundColor;
        self.tableView.tipImage = [UIImage imageNamed:@"No_order_icon"];
        self.tableView.tipView.hidden = NO;
        self.tableView.scrollEnabled = YES;
    }else{
        self.tableView.tipView.hidden = YES;
        self.tableView.scrollEnabled = YES;
    }
}


- (void)endRefreshing{
    if ([self.tableView.mj_header isRefreshing]) {
        [self.tableView.mj_header endRefreshing];
    }
    if ([self.tableView.mj_footer isRefreshing]) {
        [self.tableView.mj_footer endRefreshing];
    }

}
- (NSMutableArray *)data{
    if (!_data) {
        _data = [[NSMutableArray alloc]init];
    }
    return _data;
}



@end
